Thread: why a[i][j]=2 in this program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    why a[i][j]=2 in this program

    In the below program, I wonder what it means by a[i][j]=2 and why they have used this. Does it mean value of a[i][j]=2, then how come that value..i'm confused

    Code:
    int main (void)
      {
         int a[3][2];
         int i,j;
     
         for (i=0; j<3; i++)
           for (j=0; j<2; j++)
               a[i][j] =2;
      
         for (i=0; i<3; i++)
            for (j=0; j<2; j++)
               printf ("value in array %d\n", a[i][j]);
     
         for (i=0; i<3; i++)
            for (j=0; j<3; j++)
               print ("value in array %d and address is %P\n", a[i][j], (void*) &a[i][j]);
        
        return 0; 
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The first loop in initializing each element of the array to 2 with the line a[i][j] =2;. This value is just to show how to initialize an array. You could use any valid value in place of the 2.

    Jim

  3. #3
    Registered User oluwasegun's Avatar
    Join Date
    Nov 2011
    Location
    lagos nigeria
    Posts
    3
    the first loop structure initailize the array to 2.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    Thanks again. But what does that mean "initialize the array to 2". Does it mean [2][2] array?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    See this link on arrays.

    Jim

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by learning_grc View Post
    Thanks again. But what does that mean "initialize the array to 2". Does it mean [2][2] array?
    An array is a bunch of boxes. Each box holds something... array[6] = 2 ...means store the number 2 in box #6.

    In your case the areay is organized as 3 rows of 2 columns each... array[2][1] = 2... means go down to row 2 then over to clumn 1 and drop a 2 in there.

    For the purposes of the example in message #1... it could be any value, the point is to demonstrate how to initialize (preset) an array of boxes to a specific value, which is used in programming all the time.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Code:
         for (i=0; j<3; i++)
           for (j=0; j<2; j++)
               a[i][j] =2;
       
         for (i=0; i<3; i++)
            for (j=0; j<2; j++)
               printf ("value in array %d\n", a[i][j]);
    Beware of typo's - the first loop is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  2. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM